﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TS.Bussiness;
using TS.Commons;

namespace Tranco
{
    public partial class Home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                ConfigurationBussiness cbu = new ConfigurationBussiness();
                    var config = cbu.GetConfig();
                    if (config.IsClosed == 1)
                        Response.Redirect("~/Maintenance.aspx");
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUser.Value.ToStringOrDefault()))
            {
                var strScript = new StringBuilder();
                strScript.Append("<script language=JavaScript>");
                string scriptString = string.Format(CultureInfo.CurrentCulture, @"alert('Tên đăng nhập không được để trống.'); </script>");
                strScript.Append(scriptString);
                ClientScript.RegisterStartupScript(GetType(), "Pop", strScript.ToString());
                return;
            }

            if (string.IsNullOrEmpty(txtPass.Value.ToStringOrDefault()))
            {
                var strScript = new StringBuilder();
                strScript.Append("<script language=JavaScript>");
                string scriptString = string.Format(CultureInfo.CurrentCulture, @"alert('Mật khẩu không được để trống.'); </script>");
                strScript.Append(scriptString);
                ClientScript.RegisterStartupScript(GetType(), "Pop", strScript.ToString());
                return;
            }

            UserBussiness bu = new UserBussiness();
            var user = bu.GetUserLogOn(txtUser.Value.ToStringOrDefault(), CryptographyHelper.HashPassword(txtPass.Value.ToStringOrDefault()));
            if (user != null)
            {
                if (user.IsLock == 1)
                {
                    return;
                }
                Session["TracoUserName"] = user.UserName;
                Session["TracoGroupId"] = user.GroupId;
                Session["TracoUserId"] = user.Userid;
                Session["TracoOwnerId"] = user.OwnerId;

                Response.Redirect("~/SchedulePersonal.aspx");

            }
            else
            {
                ClientConfirm();
            }

        }

        public void ClientConfirm()
        {
            var strScript = new StringBuilder();
            strScript.Append("<script language=JavaScript>");
            string scriptString = string.Format(CultureInfo.CurrentCulture, @"alert('Sai tài khoản đăng nhập, vui lòng đăng nhập lại.'); </script>");
            strScript.Append(scriptString);
            ClientScript.RegisterStartupScript(GetType(), "Pop", strScript.ToString());
        }
    }
}